
var app = getApp()

Page({
	data: {
		items: '',
		limit: 10,
		page: 1,
		done: false,
		keywords:'',
		showmenu1: true,
		showmenu2: true,
		showmenu3: true,
		showmenu4: true,
		searchlimit:5,
		searchbnt: false
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.getDataList();
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
		this.getDataList()
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	},

	getDataList() {

		var that = this;
		var openId = wx.getStorageSync('user_new');
		var limit = that.data.limit || 10;
		var page = that.data.page || 1;
		var keywords = that.data.keywords || '';

		if (this.data.done) {

			wx.showToast({
				title: '暂无更多内容',
				duration:1000,
				icon:'none'
			})

			return false
		}

		wx.showLoading({
			title: '加载中',
		})

		app.core('get', 'newhouse/myorder/openId/' + openId +'/limit/'+limit+'?page=' + page + '&keywords=' + keywords, {},
			(res) => {

				var arr = res.data.items;

				if (arr.length == 0) {
					wx.showToast({
						title: '暂无更多内容',
						duration:1000,
						icon:'none'
					})

					if (page == 1) {

						that.setData({
							items: [],
						})
					}

					return false
				}

				for (var i = 0; i < arr.length; i++) {
					switch (arr[i].propertyType) {
						case 1:
							arr[i].propertyType = '住宅'
							break;
						case 2:
							arr[i].propertyType = '商铺'
							break;
						case 3:
							arr[i].propertyType = '写字楼'
							break;
						default:
							arr[i].propertyType = '住宅'
					}

					if (arr[i].fitment == 1) {
						arr[i].fitment = '毛坯'
					} else if (arr[i].fitment == 2) {
						arr[i].fitment = '普通装修'
					} else if (arr[i].fitment == 3) {
						arr[i].fitment = '精装修'
					} else {
						arr[i].fitment = '其他'
					}
				}

				var items = that.data.items || []

				if (page < res.data.lastPage ) {
					var _page = that.data.page + 1
				} else {
					var _page = 0;
					that.setData({
						done: true
					})
				}

				that.setData({
					items: [ ...items, ...arr],
					page: _page
				})

				wx.hideLoading()
			}
		)
	},// 二手楼盘

	searchChange(e) {
		this.setData({
			keywords: e.detail.value
		})
	},

	handleSearch() {
		this.setData({
			page: 1,
			done: false,
			items: [],
		})
		this.getDataList()
	},

	handlePay(e) {
		console.log(e.target.dataset.pid)
		if (!e.target.dataset.pid) {
			return false
		}

		let that = this
		let pid = e.target.dataset.pid

		app.core('post', 'newhouse/wxpay', { id: pid }, (res => {
			console.log(res)
			wx.requestPayment({
				'timeStamp': res.data.timeStamp,
				'nonceStr': res.data.nonceStr,
				'package': res.data.package,
				'signType': res.data.signType,
				'paySign': res.data.paySign,
				'success':function(res){
					console.log(res)
					wx.showToast({
						title: '支付成功',
						duration: 1000,
						icon: 'none'
					})

					let items = [];
					for (let i = 0; i < that.data.items.length; i++) {
						if (that.data.items[i]['id'] == pid) {
							that.data.items[i]['status'] = 1
						}
						items.push(that.data.items[i])

					}

					that.setData({
						items: items
					})
				},
				'fail':function(res){
					console.log(res)
					wx.showToast({
						title: '支付失败',
						duration: 1000,
						icon: 'none'
					})
				},
				'complete':function(res){
					console.log(res)
				}
			})
		}))
	},

	handleDetail(e) {
		console.log(e.target.dataset)
		wx.navigateTo({
			url: '/pages/loupan/zaixiandetail?id=' + e.target.dataset.pid
		})
	}

})
